package com.zhang.flink.example.source;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;

/**
 * SourceFunction底层限制并行度只能为1
 * @title: 自定义发送1000以内随机数的source
 * @author: zhang
 * @date: 2022/2/11 19:44
 */
public class IntegerSource implements SourceFunction<Integer> {
    private boolean running = true;
    private Random random = new Random();

    @Override
    public void run(SourceContext<Integer> ctx) throws Exception {
        while (running) {
            ctx.collect(random.nextInt(1000));
            Thread.sleep(1000L);
        }
    }

    @Override
    public void cancel() {
        running = false;
    }
}
